package com.tqz.rule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;

import java.util.List;
import java.util.Random;

/**
 * <p>随机的算法，获取一个服务实例
 *
 * @author tianqingzhao
 * @since 2021/7/8 12:13
 */
@Slf4j
public class RandomChooseInstanceRule implements IChooseInstanceRules {

    @Override
    public ServiceInstance chooseInstance(List<ServiceInstance> serviceInstanceList) {
        if (serviceInstanceList != null && !serviceInstanceList.isEmpty()) {

            Integer serverIndex = new Random().nextInt(serviceInstanceList.size());

            return serviceInstanceList.get(serverIndex);
        }
        return null;
    }
}
